ExternalCustomInspector.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Reflection;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace ExternPropertyAttributes.Editor
  7. {
  8. // Applied in `CustomInspectorDefinitions.cs`.
  9. public class ExternalCustomInspector : UnityEditor.Editor
  10. {
  11. private List<SerializedProperty> _serializedProperties = new List<SerializedProperty>();
  12. private IEnumerable<FieldInfo> _nonSerializedFields;
  13. private IEnumerable<PropertyInfo> _nativeProperties;
  14. private IEnumerable<MethodInfo> _methods;
  15. private Dictionary<string, SavedBool> _foldouts = new Dictionary<string, SavedBool>();
  16. private void OnEnable()
  17. {
  18. _nonSerializedFields = ReflectionUtility.GetAllFields(
  19. target, f => f.GetCustomAttributes(typeof(ShowNonSerializedFieldAttribute), true).Length > 0);
  20. _nativeProperties = ReflectionUtility.GetAllProperties(
  21. target, p => p.GetCustomAttributes(typeof(ShowNativePropertyAttribute), true).Length > 0);
  22. _methods = ReflectionUtility.GetAllMethods(
  23. target, m => m.GetCustomAttributes(typeof(ButtonAttribute), true).Length > 0);
  24. }
  25. public override void OnInspectorGUI()
  26. {
  27. GetSerializedProperties(ref _serializedProperties);
  28. bool anyExternalCustomAttribute = _serializedProperties.Any(p => PropertyUtility.GetAttribute<ICustomAttribute>(p) != null);
  29. if (!anyExternalCustomAttribute)
  30. {
  31. DrawDefaultInspector();
  32. }
  33. else
  34. {
  35. DrawSerializedProperties();
  36. }
  37. DrawNonSerializedFields();
  38. DrawNativeProperties();
  39. DrawButtons();
  40. }
  41. private void GetSerializedProperties(ref List<SerializedProperty> outSerializedProperties)
  42. {
  43. outSerializedProperties.Clear();
  44. using (var iterator = serializedObject.GetIterator())
  45. {
  46. if (iterator.NextVisible(true))
  47. {
  48. do
  49. {
  50. outSerializedProperties.Add(serializedObject.FindProperty(iterator.name));
  51. }
  52. while (iterator.NextVisible(false));
  53. }
  54. }
  55. }
  56. private void DrawSerializedProperties()
  57. {
  58. serializedObject.Update();
  59. // Draw non-grouped serialized properties
  60. foreach (var property in GetNonGroupedProperties(_serializedProperties))
  61. {
  62. if (property.name.Equals("m_Script", System.StringComparison.Ordinal))
  63. {
  64. using (new EditorGUI.DisabledScope(disabled: true))
  65. {
  66. EditorGUILayout.PropertyField(property);
  67. }
  68. }
  69. else
  70. {
  71. ExternalCustomEditorGUI.PropertyField_Layout(property, includeChildren: true);
  72. }
  73. }
  74. // Draw grouped serialized properties
  75. foreach (var group in GetGroupedProperties(_serializedProperties))
  76. {
  77. IEnumerable<SerializedProperty> visibleProperties = group.Where(p => PropertyUtility.IsVisible(p));
  78. if (!visibleProperties.Any())
  79. {
  80. continue;
  81. }
  82. ExternalCustomEditorGUI.BeginBoxGroup_Layout(group.Key);
  83. foreach (var property in visibleProperties)
  84. {
  85. ExternalCustomEditorGUI.PropertyField_Layout(property, includeChildren: true);
  86. }
  87. ExternalCustomEditorGUI.EndBoxGroup_Layout();
  88. }
  89. // Draw foldout serialized properties
  90. foreach (var group in GetFoldoutProperties(_serializedProperties))
  91. {
  92. IEnumerable<SerializedProperty> visibleProperties = group.Where(p => PropertyUtility.IsVisible(p));
  93. if (!visibleProperties.Any())
  94. {
  95. continue;
  96. }
  97. if (!_foldouts.ContainsKey(group.Key))
  98. {
  99. _foldouts[group.Key] = new SavedBool($"{target.GetInstanceID()}.{group.Key}", false);
  100. }
  101. _foldouts[group.Key].Value = EditorGUILayout.Foldout(_foldouts[group.Key].Value, group.Key, true);
  102. if (_foldouts[group.Key].Value)
  103. {
  104. foreach (var property in visibleProperties)
  105. {
  106. ExternalCustomEditorGUI.PropertyField_Layout(property, true);
  107. }
  108. }
  109. }
  110. serializedObject.ApplyModifiedProperties();
  111. }
  112. private void DrawNonSerializedFields(bool drawHeader = false)
  113. {
  114. if (_nonSerializedFields.Any())
  115. {
  116. if (drawHeader)
  117. {
  118. EditorGUILayout.Space();
  119. EditorGUILayout.LabelField("Non-Serialized Fields", GetHeaderGUIStyle());
  120. ExternalCustomEditorGUI.HorizontalLine(
  121. EditorGUILayout.GetControlRect(false), HorizontalLineAttribute.DefaultHeight, HorizontalLineAttribute.DefaultColor.GetColor());
  122. }
  123. foreach (var field in _nonSerializedFields)
  124. {
  125. ExternalCustomEditorGUI.NonSerializedField_Layout(serializedObject.targetObject, field);
  126. }
  127. }
  128. }
  129. private void DrawNativeProperties(bool drawHeader = false)
  130. {
  131. if (_nativeProperties.Any())
  132. {
  133. if (drawHeader)
  134. {
  135. EditorGUILayout.Space();
  136. EditorGUILayout.LabelField("Native Properties", GetHeaderGUIStyle());
  137. ExternalCustomEditorGUI.HorizontalLine(
  138. EditorGUILayout.GetControlRect(false), HorizontalLineAttribute.DefaultHeight, HorizontalLineAttribute.DefaultColor.GetColor());
  139. }
  140. foreach (var property in _nativeProperties)
  141. {
  142. ExternalCustomEditorGUI.NativeProperty_Layout(serializedObject.targetObject, property);
  143. }
  144. }
  145. }
  146. private void DrawButtons(bool drawHeader = false)
  147. {
  148. if (_methods.Any())
  149. {
  150. if (drawHeader)
  151. {
  152. EditorGUILayout.Space();
  153. EditorGUILayout.LabelField("Buttons", GetHeaderGUIStyle());
  154. ExternalCustomEditorGUI.HorizontalLine(
  155. EditorGUILayout.GetControlRect(false), HorizontalLineAttribute.DefaultHeight, HorizontalLineAttribute.DefaultColor.GetColor());
  156. }
  157. foreach (var method in _methods)
  158. {
  159. ExternalCustomEditorGUI.Button(serializedObject.targetObject, method);
  160. }
  161. }
  162. }
  163. private static IEnumerable<SerializedProperty> GetNonGroupedProperties(IEnumerable<SerializedProperty> properties)
  164. {
  165. return properties.Where(p => PropertyUtility.GetAttribute<IGroupAttribute>(p) == null);
  166. }
  167. private static IEnumerable<IGrouping<string, SerializedProperty>> GetGroupedProperties(IEnumerable<SerializedProperty> properties)
  168. {
  169. return properties
  170. .Where(p => PropertyUtility.GetAttribute<BoxGroupAttribute>(p) != null)
  171. .GroupBy(p => PropertyUtility.GetAttribute<BoxGroupAttribute>(p).Name);
  172. }
  173. private static IEnumerable<IGrouping<string, SerializedProperty>> GetFoldoutProperties(IEnumerable<SerializedProperty> properties)
  174. {
  175. return properties
  176. .Where(p => PropertyUtility.GetAttribute<FoldoutAttribute>(p) != null)
  177. .GroupBy(p => PropertyUtility.GetAttribute<FoldoutAttribute>(p).Name);
  178. }
  179. private static GUIStyle GetHeaderGUIStyle()
  180. {
  181. GUIStyle style = new GUIStyle(EditorStyles.centeredGreyMiniLabel);
  182. style.fontStyle = FontStyle.Bold;
  183. style.alignment = TextAnchor.UpperCenter;
  184. return style;
  185. }
  186. }
  187. }